package org.fmi.lje.graphics.diagrams;

import java.util.Collection;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;

import org.apache.batik.svggen.SVGGraphics2D;
import org.fmi.lje.config.ProjectConfiguration;
import org.fmi.lje.graphics.RelationshipsLayoutManager;
import org.fmi.lje.graphics.model.Canvas;
import org.fmi.lje.graphics.model.Clazz;
import org.fmi.lje.graphics.model.Shape;
import org.fmi.lje.uml.svg.SvgDiagram;

import com.google.inject.Inject;

public class ClassHierarchyDiagram extends Canvas implements SvgDiagram {

	private RelationshipsLayoutManager layoutManager;

	private Map<String, Shape> shapesMap = new Hashtable<String, Shape>();

	@Inject
	public ClassHierarchyDiagram(SVGGraphics2D graphics, RelationshipsLayoutManager layoutManager) {
		super(graphics, 0, 0);

		this.layoutManager = layoutManager;
	}

	public RelationshipsLayoutManager getLayoutManager() {
		return layoutManager;
	}

	public Clazz createShape(Class<?> clazz) {
		Clazz shape = ProjectConfiguration.getInjector().getInstance(Clazz.class);
		shape.setParent(this);
		shape.setTarget(clazz);

		shapes.add(shape);
		addShape(shape, clazz.toString());

		return shape;
	}

	@Override
	public void addShape(Shape shape, String id) {
		assert shape instanceof Clazz;

		shapesMap.put(id, (Clazz) shape);
	}

	@Override
	public Shape getShapeById(String id) {
		return shapesMap.get(id);
	}

	public void addAll(Collection<? extends Shape> allShapes) {
		for (Shape shape : allShapes) {
			if (shape != null) {
				shapes.add(shape);

				shapesMap.put(shape.toString(), shape);
			}
		}
	}
}
